package com.th.supcom.pds.prescstatusquery.client.widget;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.KeyListener;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.KeyNav;
import com.extjs.gxt.ui.client.widget.Component;
import com.th.supcom.pds.common.client.commom.FocusEvents;
import com.th.supcom.pds.common.client.model.PdsSupplyProModel;
import com.th.supcom.pds.common.constant.FocusConstants;
import com.th.supcom.pds.common.constant.KeysConstants;
import com.th.supcom.pds.common.utils.ClientUtils;

/**
 * 核对发药时间监听
 * 
 * @author 杨冲
 */
public class SupplyStatusQueryListener
{
    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";
    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";
    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";
    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";
    private Component m_component;
    private Component m_focusComponent;
    private FocusEvents m_focusEvents;

    private SupplyStatusQueryAction m_prescStatusQueryAction;

    private SupplyStatusQueryButtons m_prescStatusQueryButtons;

    private SupplyStatusQueryFields m_prescStatusQueryFields;

    private SupplyStatusQueryPrescGrid m_prescStatusQueryPrescGrid;

    /**
     * 添加清屏事件，当点击清屏按钮时，触发此事件
     */
    private void addClearListener ()
    {
        m_prescStatusQueryButtons.getClearBtn ().addSelectionListener (new SelectionListener <ButtonEvent> ()
        {

            @Override
            public void componentSelected (final ButtonEvent ce)
            {
                m_prescStatusQueryAction.clear (true);
            }

        });
    }

    /**
     * 单击处方主记录Grid触发此事件
     */
    private void addClickGridListener ()
    {
        m_prescStatusQueryPrescGrid.getGrid ().getGrid ().getSelectionModel ().addSelectionChangedListener (new SelectionChangedListener <PdsSupplyProModel> ()
        {

            @Override
            public void selectionChanged (SelectionChangedEvent <PdsSupplyProModel> se)
            {
                if (null == se.getSelectedItem ())
                {
                    return;
                }
                m_prescStatusQueryAction.doSelectedChange (se.getSelectedItem ());
            }
        });
    }

    /**
     * 添加快捷键事件
     */
    private void addEnterKeyListener ()
    {
        m_prescStatusQueryFields.getConsumeProofField ().addKeyListener (new KeyListener ()
        {
            @Override
            public void componentKeyDown (ComponentEvent event)
            {
                if (event.getKeyCode () == KeysConstants.VK_ENTER)
                {
                    m_prescStatusQueryAction.findPrescByCondition (m_prescStatusQueryFields.getConsumeProofField ().getValue (), m_prescStatusQueryFields.getRecipeNoField ().getValue ());
                }
            }
        });
        m_prescStatusQueryFields.getRecipeNoField ().addKeyListener (new KeyListener ()
        {
            @Override
            public void componentKeyDown (ComponentEvent event)
            {
                if (event.getKeyCode () == KeysConstants.VK_ENTER)
                {
                    m_prescStatusQueryAction.findPrescByCondition (m_prescStatusQueryFields.getConsumeProofField ().getValue (), m_prescStatusQueryFields.getRecipeNoField ().getValue ());
                }
            }
        });
        new KeyNav <ComponentEvent> (m_component)
        {
            @Override
            public void onKeyPress (final ComponentEvent ce)
            {
                final int keyCode = ce.getKeyCode ();

                if (keyCode == KeysConstants.VK_ENTER)
                {
                    executeEnterEvent ();
                }

                if (!ce.isAltKey ())
                {
                    return;
                }

                executeAltEvent (keyCode);

            }
        };
    }

    /**
     * 监听焦点事件
     */
    private void addFocusListeners ()
    {
        m_focusEvents = new FocusEvents ();
        m_focusEvents.add (FocusConstants.PRESC_STATUS_QUERY_CLEAR_BTN, m_prescStatusQueryButtons.getClearBtn ());
        m_focusEvents.add (FocusConstants.PRESC_STATUS_QUERY_PRESC_NO, m_prescStatusQueryFields.getRecipeNoField ());
        m_focusEvents.add (FocusConstants.PRESC_STATUS_QUERY_QUERY_BTN, m_prescStatusQueryButtons.getQueryBtn ());
        m_focusEvents.add (FocusConstants.PRESC_STATUS_QUERY_RCPT_NO, m_prescStatusQueryFields.getConsumeProofField ());

        ClientUtils.getFocusChange (m_prescStatusQueryButtons.getQueryBtn (), null, m_prescStatusQueryButtons.getClearBtn ());
        ClientUtils.getFocusChange (m_prescStatusQueryFields.getRecipeNoField (), m_prescStatusQueryButtons.getClearBtn (), m_prescStatusQueryButtons.getQueryBtn ());
    }

    /**
     * 添加查询处方明细事件，当点击处方主表时，触发此事件
     */
    private void addQueryPrescDetailListener ()
    {
        m_prescStatusQueryButtons.getQueryBtn ().addSelectionListener (new SelectionListener <ButtonEvent> ()
        {

            @Override
            public void componentSelected (final ButtonEvent ce)
            {
                m_prescStatusQueryAction.findPrescByCondition (m_prescStatusQueryFields.getConsumeProofField ().getValue (), m_prescStatusQueryFields.getRecipeNoField ().getValue ());
            }
        });
    }

    /**
     * 处理Alt+?快捷键事件
     * 
     * @param keyCode
     */
    private void executeAltEvent (final int keyCode)
    {
        if (keyCode == KeysConstants.VK_C && null != m_focusComponent)
        {
            m_component.hide ();
            m_focusComponent.focus ();
            return;
        }
        if (keyCode == KeysConstants.VK_R)
        {
            m_prescStatusQueryAction.clear (true);
        }

        if (keyCode == KeysConstants.VK_Q)
        {
            m_prescStatusQueryAction.findPrescByCondition (m_prescStatusQueryFields.getConsumeProofField ().getValue (), m_prescStatusQueryFields.getRecipeNoField ().getValue ());
        }
    }

    /**
     * 执行回车事件
     */
    private void executeEnterEvent ()
    {

    }

    /**
     * 初始化所有的Listener
     */
    public void initListeners ()
    {
        addQueryPrescDetailListener ();
        addClearListener ();
        addEnterKeyListener ();
        addFocusListeners ();
        addClickGridListener ();
    }

    public void setFocusComponent (final Component focusComponent)
    {
        m_focusComponent = focusComponent;
    }

    public void setKeyNavComponent (final Component component)
    {
        m_component = component;
    }

    public void setPrescStatusQueryAction (final SupplyStatusQueryAction prescStatusQueryAction)
    {
        m_prescStatusQueryAction = prescStatusQueryAction;
    }

    public void setPrescStatusQueryButtons (final SupplyStatusQueryButtons prescStatusQueryButtons)
    {
        m_prescStatusQueryButtons = prescStatusQueryButtons;
    }

    public void setPrescStatusQueryFields (final SupplyStatusQueryFields prescStatusQueryFields)
    {
        m_prescStatusQueryFields = prescStatusQueryFields;
    }

    public void setPrescStatusQueryPrescGrid (final SupplyStatusQueryPrescGrid prescStatusQueryPrescGrid)
    {
        m_prescStatusQueryPrescGrid = prescStatusQueryPrescGrid;
    }
}
